import { getCaptcha } from '@/api/user'
export default {
  data() {
    return {}
  },
  created() {
    this.refreshCode()
  },
  methods: {
    refreshCode() {
      if (!this.website.captchaMode) {
        return
      }
      getCaptcha().then(res => {
        const { data = {}} = res
        const dataObj = data.data || {}
        this.loginForm.key = dataObj.codeKey
        this.loginForm.image = dataObj.imageBase64Data
      })
    },
    befroeHandleLogin() {},
    handleLogin() {
      this.befroeHandleLogin().then(formData => {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.UserLogin(formData)
          }
        })
      })
    },
    UserLogin(formData) {
      const loading = this.$loading({
        lock: true,
        text: '登录中,请稍后。。。',
        spinner: 'el-icon-loading'
      })
      this.$store.dispatch('LoginByUsername', formData).then(() => {
        this.$router.push('/')
        loading.close()
      }).catch(() => {
        loading.close()
        this.refreshCode()
      })
    }
  }
}
